/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package helpdiabetesj2me;

import java.util.Date;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author Johan VM Windows 7
 */
public class AddInsulinEvent extends MyCanvas implements CommandListener {

    Command Terug, VoegToe;
    static final String[] str_terug = {"Terug"};
    static final String[] str_kies_hoeveelheid = {"Aantal eenheden :"};
    static final String[] str_voegtoe = {"VoegToe"};
    static final String[] str_time = {"Tijdstip :"};
    static final String[] str_duration_in_minutes = {"Duration :"};
    
    Displayable curdisp;
    int insulinAmountFloor = 5;
    int insulinAmountFraction = 0;
    int recordId;//if -1 then its actually a new event being added
    
    String Hour1;//first digit of the hours, eg if hour = 9, then this digit is 0, if hour = 13 this digit = 1
    String Hour2;//second digit
    String Minute1;//first digit of the minutes
    String Minute2;//second digit of the minutes
    int highlightedField;//0 amountflor, 1 is amountfraction, 2  is Hour1 is selected, ... op to 5 = Minute2, 6 = durationInMinutesHour1 ... 9
    
    String durationInMinutesHour1;
    String durationInMinutesHour2;
    String durationInMinutesMinutes1;
    String durationInMinutesMinutes2;
    
    /**
     * if date = null then date will get value now<br>
     * recordId : if a new event must be added then recordId must be set to -1, else recordId is the id of the record to be changed.
     */
    public AddInsulinEvent(Font userfont, HelpDiabetes midlet,Date date, int recordId) {
        super(userfont,midlet);
        Date dateOfTheEvent;
        int durationInMinutes;
        this.recordId = recordId;
        
        if (recordId == -1) {
            if (date == null)
                dateOfTheEvent = new Date();
            else 
                dateOfTheEvent = date;
            durationInMinutes = 0;
        } else {
            ActiveInsulinEvent theEvent = ActiveInsulinManagement.getActiveInsulinEvent(recordId);
            dateOfTheEvent = theEvent.getDate();
            float amount = theEvent.getAmount();
            insulinAmountFloor = (int)amount;
            insulinAmountFraction = (int)((amount - (float)insulinAmountFloor) * 10);
            durationInMinutes = theEvent.getDurationInMinutes();
        }

        String dateAsString = Utilities.getHHmmAsString(dateOfTheEvent);
        String [] splitted = Utilities.split(dateAsString, ":");
        Hour1 = splitted[0].substring(0, 1);
        Hour2 = splitted[0].substring(1, 2);
        Minute1 = splitted[1].substring(0, 1);
        Minute2 = splitted[1].substring(1, 2);
        
        durationInMinutesHour1 = new Integer(durationInMinutes / 60 / 10).toString();
        //System.out.println(durationInMinutesHour1);
        durationInMinutesHour2 = new Integer((int)(((float)durationInMinutes / 60 / 10  - java.lang.Float.parseFloat(durationInMinutesHour1)) * 10)).toString();
        //System.out.println(durationInMinutesHour2);
        durationInMinutesMinutes1 = new Integer((new Integer(durationInMinutes  - (Integer.parseInt(durationInMinutesHour1) * 10 + Integer.parseInt(durationInMinutesHour2)) * 60)).intValue()/10).toString();
        //System.out.println(durationInMinutesMinutes1);
        durationInMinutesMinutes2 = new Integer(durationInMinutes - ((Integer.parseInt(durationInMinutesHour1) * 10 + Integer.parseInt(durationInMinutesHour2)) * 60) - Integer.parseInt(durationInMinutesMinutes1) * 10).toString();
        //System.out.println(durationInMinutesMinutes2);

        Terug = new Command(str_terug[HelpDiabetes.languageIndex],str_terug[HelpDiabetes.languageIndex],Command.EXIT,1);
        this.addCommand(Terug);
        VoegToe = new Command(str_voegtoe[HelpDiabetes.languageIndex],Command.OK,2);
        this.addCommand(VoegToe);
        this.setCommandListener(this);
        curdisp = midlet.getDisplay().getCurrent();
        midlet.getDisplay().setCurrent(this);
    }

    protected void paint(Graphics graphics) {
        x=0;y=0;
        graphics.setFont(userfont);

        graphics.setColor(255,255,255);
        graphics.fillRect(0, 0, scrwidth, scrhght);
        
        //print the amount
        paintstring((" " + str_kies_hoeveelheid[HelpDiabetes.languageIndex] + " ").getBytes(),false,graphics,userfont);
        x = 0; y = y + fonthght;
        paintstring("  ".getBytes(),false,graphics,userfont);
        paintstring(("" + insulinAmountFloor).getBytes(),highlightedField == 0,graphics,userfont);
        paintstring(".".getBytes(),false,graphics,userfont);
        paintstring((insulinAmountFraction + "").getBytes(),highlightedField == 1,graphics,userfont);
        
        //add a blank line
        x = 0; y = y + fonthght;
        y = y + fonthght;
        paintstring(str_time[0].getBytes(), false, graphics, userfont);
        x = 0; y = y + fonthght;
        paintstring("  ".getBytes(), false, graphics, userfont);
        paintstring(Hour1.getBytes(), highlightedField == 2, graphics, userfont);
        paintstring(Hour2.getBytes(), highlightedField == 3, graphics, userfont);
        paintstring(":".getBytes(), false, graphics, userfont);
        paintstring(Minute1.getBytes(), highlightedField == 4, graphics, userfont);
        paintstring(Minute2.getBytes(), highlightedField == 5, graphics, userfont);

    
        //add a blank line
        x = 0; y = y + fonthght;
        y = y + fonthght;
        paintstring(str_duration_in_minutes[0].getBytes(), false, graphics, userfont);
        x = 0; y = y + fonthght;
        paintstring("  ".getBytes(), false, graphics, userfont);
        paintstring(durationInMinutesHour1.getBytes(), highlightedField == 6, graphics, userfont);
        paintstring(durationInMinutesHour2.getBytes(), highlightedField == 7, graphics, userfont);
        paintstring(":".getBytes(), false, graphics, userfont);
        paintstring(durationInMinutesMinutes1.getBytes(), highlightedField == 8, graphics, userfont);
        paintstring(durationInMinutesMinutes2.getBytes(), highlightedField == 9, graphics, userfont);
    }

    public void commandAction(Command command, Displayable d) {
        if (command == Terug) {
            midlet.getDisplay().setCurrent(curdisp);
        } else if (command == VoegToe) {
            addItem();
        }
    }
    
    public synchronized void keyPressed (int keyCode) {
        if ((keyCode > (KEY_NUM0 -1)) && (keyCode < (KEY_NUM9 + 1))) {
            if (highlightedField == 0)
                insulinAmountFloor =  keyCode - KEY_NUM0;
            else if (highlightedField == 1)
                insulinAmountFraction = keyCode - KEY_NUM0;
            else if (highlightedField == 2)  {
                if (keyCode - KEY_NUM0 < 3)
                    Hour1 = Integer.toString(keyCode - KEY_NUM0);
            }
            else if (highlightedField == 3)  {
                if ((Hour1.equalsIgnoreCase("2") && keyCode - KEY_NUM0 < 4) || (Hour1.equalsIgnoreCase("0")  || Hour1.equalsIgnoreCase("1") ))
                    Hour2 = Integer.toString(keyCode - KEY_NUM0);
            }
            else if (highlightedField == 4)  {
                if (keyCode - KEY_NUM0 < 6)
                    Minute1 = Integer.toString(keyCode - KEY_NUM0);
            }
            else if (highlightedField == 5)  {
                Minute2 = Integer.toString(keyCode - KEY_NUM0);
            } else if (highlightedField == 6)  {
                if (keyCode - KEY_NUM0 < 3)
                    durationInMinutesHour1 = Integer.toString(keyCode - KEY_NUM0);
            }
            else if (highlightedField == 7)  {
                if ((durationInMinutesHour1.equalsIgnoreCase("2") && keyCode - KEY_NUM0 < 4) || (durationInMinutesHour1.equalsIgnoreCase("0")  || durationInMinutesHour1.equalsIgnoreCase("1") ))
                    durationInMinutesHour2 = Integer.toString(keyCode - KEY_NUM0);
            }
            else if (highlightedField == 8)  {
                if (keyCode - KEY_NUM0 < 6)
                    durationInMinutesMinutes1 = Integer.toString(keyCode - KEY_NUM0);
            }
            else if (highlightedField == 9)  {
                durationInMinutesMinutes2 = Integer.toString(keyCode - KEY_NUM0);
            }
             
            if (highlightedField < 9)
                highlightedField++;
        } else {
            switch(getGameAction(keyCode)) {
                case LEFT:
                    if (highlightedField == 0)
                        midlet.getDisplay().setCurrent(curdisp);
                    else 
                        highlightedField--;
                    break;
                case RIGHT:
                    if (highlightedField < 9)
                        highlightedField++;
                    break;
                case UP:
                    switch (highlightedField) {
                        case 0:
                            if (insulinAmountFloor == 9)
                                insulinAmountFloor = 0;
                            else 
                                insulinAmountFloor++;
                            break;
                        case 1:
                            if (insulinAmountFraction == 5) {
                                insulinAmountFraction = 0;
                                if (insulinAmountFloor == 9)
                                    insulinAmountFloor = 0;
                                else 
                                    insulinAmountFloor++;
                            }
                            else 
                                insulinAmountFraction = 5;
                            break;
                        case 2:
                            if (Hour1.equalsIgnoreCase("2"))
                                Hour1 = "0";
                            else {
                                Hour1 = new String(new char[] {(char)(Hour1.charAt(0) + 1)});
                                if (Hour1.equalsIgnoreCase("2")) {
                                    if (Hour2.charAt(0) > "3".charAt(0))
                                        Hour2 = "3";
                                }
                            }
                            break;
                        case 3:
                            String maxValue = Hour1.equalsIgnoreCase("2") ? "3":"9";
                            if (Hour2.equalsIgnoreCase(maxValue))
                                Hour2 = "0";
                            else
                                Hour2 = new String(new char[] {(char)(Hour2.charAt(0) + 1)});
                            break;
                        case 4:
                            if (Minute1.equalsIgnoreCase("5"))
                                Minute1 = "0";
                            else
                                Minute1 = new String(new char[] {(char)(Minute1.charAt(0) + 1)});
                            break;
                        case 5:
                            if (Minute2.equalsIgnoreCase("9"))
                                Minute2 = "0";
                            else
                                Minute2 = new String(new char[] {(char)(Minute2.charAt(0) + 1)});
                            break;
                        case 6:
                            if (durationInMinutesHour1.equalsIgnoreCase("2"))
                                durationInMinutesHour1 = "0";
                            else {
                                durationInMinutesHour1 = new String(new char[] {(char)(durationInMinutesHour1.charAt(0) + 1)});
                                if (durationInMinutesHour1.equalsIgnoreCase("2")) {
                                    if (durationInMinutesHour2.charAt(0) > "3".charAt(0))
                                        durationInMinutesHour2 = "3";
                                }
                            }
                            break;
                        case 7:
                            String maxValue2 = durationInMinutesHour1.equalsIgnoreCase("2") ? "3":"9";
                            if (durationInMinutesHour2.equalsIgnoreCase(maxValue2))
                                durationInMinutesHour2 = "0";
                            else
                                durationInMinutesHour2 = new String(new char[] {(char)(durationInMinutesHour2.charAt(0) + 1)});
                            break;
                        case 8:
                            if (durationInMinutesMinutes1.equalsIgnoreCase("5"))
                                durationInMinutesMinutes1 = "0";
                            else
                                durationInMinutesMinutes1 = new String(new char[] {(char)(durationInMinutesMinutes1.charAt(0) + 1)});
                            break;
                        case 9:
                            if (durationInMinutesMinutes2.equalsIgnoreCase("9"))
                                durationInMinutesMinutes2 = "0";
                            else
                                durationInMinutesMinutes2 = new String(new char[] {(char)(durationInMinutesMinutes2.charAt(0) + 1)});
                            break;
                    }
                    repaint();
                    break;
                case DOWN:
                    switch (highlightedField) {
                        case 0:
                            if (insulinAmountFloor == 0)
                                insulinAmountFloor = 9;
                            else 
                                insulinAmountFloor--;
                            break;
                        case 1:
                            if (insulinAmountFraction == 5)
                                insulinAmountFraction = 0;
                            else {
                                insulinAmountFraction = 5;
                                if (insulinAmountFloor == 0)
                                    insulinAmountFloor = 9;
                                else 
                                    insulinAmountFloor--;
                            }
                            break;
                        case 2:
                            if (Hour1.equalsIgnoreCase("0")) {
                                Hour1 = "2";
                                if (Hour2.charAt(0) > "3".charAt(0))
                                    Hour2 = "3";
                            } else
                                Hour1 = new String(new char[] {(char)(Hour1.charAt(0) - 1)});
                            break;
                        case 3:
                            String maxValue = Hour1.equalsIgnoreCase("2") ? "3":"9";
                            if (Hour2.equalsIgnoreCase("0"))
                                Hour2 = maxValue;
                            else
                                Hour2 = new String(new char[] {(char)(Hour2.charAt(0) - 1)});
                            break;
                        case 4:
                            if (Minute1.equalsIgnoreCase("0"))
                                Minute1 = "5";
                            else
                                Minute1 = new String(new char[] {(char)(Minute1.charAt(0) - 1)});
                            break;
                        case 5:
                            if (Minute2.equalsIgnoreCase("0"))
                                Minute2 = "9";
                            else
                                Minute2 = new String(new char[] {(char)(Minute2.charAt(0) - 1)});
                            break;
                    }
                    repaint();
                    break;
                case FIRE:
                    addItem();
                    break;
            }
        }
    }

    String GetClassName() {
        return("AddInsulinEvent");
    }
    
    private void addItem() {
        Date dateOfTheEvent = Utilities.parseDate(Utilities.getYearAndDateAsString(new Date()) + "T" + Hour1 + Hour2 + ":" + Minute1 + Minute2 + ":00.0");
        int durationInMinutes = (Integer.parseInt(durationInMinutesHour1) * 10 + Integer.parseInt(durationInMinutesHour2)) * 60 + (Integer.parseInt(durationInMinutesMinutes1) * 10 + Integer.parseInt(durationInMinutesMinutes2));
        ActiveInsulinManagement.addOrUpdateActiveInsulinEvent((new ActiveInsulinEventBuilder()
                .setAmount((float)insulinAmountFloor + ((float)insulinAmountFraction/10))
                .setDate(dateOfTheEvent)
                .setDurationInMinutes(durationInMinutes)
                .createActiveInsulinEvent()
                ), recordId);
        ShowActiveInsulin.activeInsulinEventsChanged = true;
        midlet.getDisplay().setCurrent(curdisp);
    }
}
